package com.itfreer.bpm.workflow.option;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.bpm.flow.api.IBpmProjectEntity;
import com.itfreer.bpm.history.entity.BpmHistoryEntity;
import com.itfreer.bpm.option.para.OptionInfo;
import com.itfreer.bpm.option.para.TaskInfo;
import com.itfreer.bpm.power.para.UserInfo;
import com.itfreer.bpm.workflow.drive.IWorkFlowDrive;
import com.itfreer.bpm.workflow.utils.BpmHistoryEditUtil;
import com.itfreer.bpm.workflow.utils.BpmHistoryQueryUtil;
import com.itfreer.bpm.workflow.utils.BpmNextTaskUtils;
/**
 * 普通节点提交操作
 * @author gj
 *
 */
@Component
public class SignCommonTaskExtends {
	
	/**
	 * 流程驱动服务
	 */
	@Autowired
	protected IWorkFlowDrive workFlowDrive;
	
	/**
	 * 构建历史帮转工具
	 */
	@Autowired
	BpmHistoryEditUtil historyEditUtl;
	
	
	/**
	 * 审批过程查询接口
	 */
	@Autowired
	BpmHistoryQueryUtil historyQueryUtil;
	
	
	/**
	 * 下一个环节工具
	 */
	@Autowired
	BpmNextTaskUtils nextTaskUtils;
	
	public void SignCommonTask(OptionInfo oInfo, IBpmProjectEntity entity, UserInfo user, String o_sassignees,
			BpmHistoryEntity overWrokH, String fromTaskName, String fromTaskID, HashMap<String, Object> ComValue) {
		for(TaskInfo itask:oInfo.getcTask()) {
			workFlowDrive.SignCommonTask(oInfo.getSexeid(), user,itask,ComValue);
		}
		HashMap<String, Object> where=new HashMap<String, Object>();
		where.put("taskdefid:=",  oInfo.getNextTask().get(0).getTaskdefinedid());
		List<BpmHistoryEntity> oldh=historyQueryUtil.getWorkEntitys(null, where, null, 1, 1);
		BpmHistoryEntity history1 =null;
		//d、设置项目待审批人信息
		String tsassignees="";
		if(oldh!=null && oldh.size()>0) {

			//3、第一个节点历史配置
			 history1 =oldh.get(0);
			 
			 tsassignees=history1.getToptassigneese();
			//项目来源节点处理
			 if(fromTaskID.equals(history1.getfromtaskdefid())
					 || (history1.getfromtaskdefid()!=null && history1.getfromtaskdefid().contains(fromTaskID))) {
				 
			 }else {
				 if(history1.getfromtaskdefid()==null) {
						//a、设置项目来源信息
						history1.setfromtaskdefname(fromTaskName);
						history1.setfromtaskdefid(fromTaskID);
				 }else {
						//a、设置项目来源信息
						history1.setfromtaskdefname(history1.getfromtaskdefname()+","+fromTaskName);
						history1.setfromtaskdefid(history1.getfromtaskdefname()+","+fromTaskID);
				 }
			 }
			//用户处理
			 if(user.getUserId().equals(history1.getfromassigneesid())
					 || (history1.getfromassigneesid()!=null && history1.getfromassigneesid().contains(user.getUserId()))) {
				 
			 }else {
				 if(history1.getfromassigneesid()==null) {
						//a、设置项目来源信息
						history1.setfromassigneesid(user.getUserId());
						history1.setfromassigneesname(user.getUserName());
				 }else {
						//a、设置项目来源信息
						history1.setfromassigneesid(history1.getfromassigneesid()+","+user.getUserId());
						history1.setfromassigneesname(history1.getfromassigneesname()+","+user.getUserName());
				 }
			 }
			 //组织机构处理
			 if(user.getUserOrgCode().equals(history1.getfromorgid())
					 || (history1.getfromorgid()!=null && history1.getfromorgid().contains(user.getUserOrgCode()))) {
				 
			 }else {
				 if(history1.getfromorgid()==null) {
						//a、设置项目来源信息
						history1.setfromorgid(user.getUserOrgCode());
						history1.setfromorgname(user.getUserOrgName());
				 }else {
						//a、设置项目来源信息
						history1.setfromassigneesid(history1.getfromorgid()+","+user.getUserOrgCode());
						history1.setfromassigneesname(history1.getfromorgname()+","+user.getUserOrgName());
				 }
			 }
		}else {

			//3、第一个节点历史配置
			 history1 =historyEditUtl.CreateHistory(oInfo.getSexeid(), overWrokH.getactdefid(), user, oInfo.getNextTask().get(0), entity);
			//a、设置项目来源信息
			history1.setfromtaskdefname(fromTaskName);
			history1.setfromtaskdefid(fromTaskID);
			
			history1.setfromassigneesid(user.getUserId());
			history1.setfromassigneesname(user.getUserName());
			history1.setfromorgid(user.getUserOrgCode());
			history1.setfromorgname(user.getUserOrgName());
		}
		//b、设置项目历史状态信息
		history1.sethistorystatus("21");
		//c、设置项目历史审批人问题
		String sassignees=","+user.getUserId()+","+user.getUserName();
		
		//d、项目已办人处理
		if(o_sassignees==null || !o_sassignees.contains(sassignees)) {
			sassignees=sassignees+","+o_sassignees;
		}
		history1.setsassignees(sassignees);
		
		//f、项目代办人处理
		List<UserInfo> userlst=oInfo.getNextTask().get(0).getUser();
		for(UserInfo iuser:userlst) {
			if(iuser.getUserSelected() && !tsassignees.contains(tsassignees+","+iuser.getUserId()+",")) {
				tsassignees=tsassignees+","+iuser.getUserId()+","+iuser.getUserName()+",";
			}
		}
		history1.setToptassigneese(tsassignees);
		
		history1.setSread("0");
		
		//e、设置项目的下一个环节
		history1.setTasktype(workFlowDrive.getTaskStringType(oInfo.getbpmkey(),  oInfo.getNextTask().get(0).getTaskdefinedid()));
		history1.setNexttask(this.nextTaskUtils.getNextTaskInfo(oInfo.getSexeid(), oInfo.getbpmkey(),oInfo.getNextTask().get(0).getTaskdefinedid(),entity).getNextTaskName());
		historyEditUtl.addandSaveEntity(history1);
		//TODO 项目时间统计计算可以在这里处理
	}
}
